package main

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"net"
	"strconv"
	"time"

	"./pipelineMiddleWare"
)

func intToByte(n int) []byte {
	data := int64(n)
	bytebuffer := bytes.NewBuffer([]byte{})

	binary.Write(bytebuffer, binary.BigEndian, data)

	return bytebuffer.Bytes()
}

func byteToInt(bts []byte) int {

	bytebuffer := bytes.NewBuffer(bts)
	var data int64
	binary.Read(bytebuffer, binary.BigEndian, &data)

	return int(data)
}

func main() {

	arrList := [][]int{{1, 9, 2, 8, 7, 3, 5, 6, 10, 4, 23, 24}, {11, 19, 12, 18, 17, 13, 15, 16, 101, 14, 123, 124}}

	sortResults := []<-chan int{}

	// fmt.Println(len(arrList))

	for i := 0; i < len(arrList); i++ {
		tcpaddr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:700"+strconv.Itoa((i+1)))
		if err != nil {
			panic(err)
		}

		conn, err := net.DialTCP("tcp", nil, tcpaddr) //链接
		if err != nil {
			panic(err)
		}

		SendArray(arrList[i], conn)

		sortResults = append(sortResults, ServerMsgHandler(conn))
	}

	// last := pipelineMiddleWare.MergeN(sortResults...)

	last := pipelineMiddleWare.Merge(sortResults[0], sortResults[1])

	for v := range last {
		fmt.Println(v)
	}

	time.Sleep(10 * time.Second)

	/*
		tcpaddr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:8845")
		if err != nil {
			panic(err)
		}

		conn, err := net.DialTCP("tcp", nil, tcpaddr) //链接
		if err != nil {
			panic(err)
		}

		go ServerMsgHandler(conn)

		arr := []int{1, 9, 2, 82, 7, 13, 5, 66, 10, 4, 23, 24, 1045, 123, 4, 6}
		length := len(arr)

		sendStart := intToByte(0)
		sendStart = append(sendStart, intToByte(0)...)
		conn.Write(sendStart)

		for i := 0; i < length; i++ {

			// fmt.Println(arr[i])

			sendData := intToByte(1)
			sendData = append(sendData, intToByte(arr[i])...)
			conn.Write(sendData)
		}

		sendEnd := intToByte(0)
		sendEnd = append(sendEnd, intToByte(1)...)
		conn.Write(sendEnd)

		time.Sleep(10 * time.Second)
	*/
}

func SendArray(arr []int, conn net.Conn) {
	length := len(arr)

	sendStart := intToByte(0)
	sendStart = append(sendStart, intToByte(0)...)
	conn.Write(sendStart)

	for i := 0; i < length; i++ {

		// fmt.Println(arr[i])

		sendData := intToByte(1)
		sendData = append(sendData, intToByte(arr[i])...)
		conn.Write(sendData)
	}

	sendEnd := intToByte(0)
	sendEnd = append(sendEnd, intToByte(1)...)
	conn.Write(sendEnd)
}

func ServerMsgHandler(conn net.Conn) <-chan int {

	out := make(chan int, 1024)

	buf := make([]byte, 16)
	defer conn.Close()
	arr := []int{} //数组保存数据

	for {
		n, err := conn.Read(buf)
		if err != nil {
			fmt.Println("client close ")
			return nil
		} else {
			if n == 16 {
				data1 := byteToInt(buf[:len(buf)/2])
				data2 := byteToInt(buf[len(buf)/2:])

				fmt.Println(data1, data2)

				if data1 == 0 && data2 == 0 {
					arr = make([]int, 0, 0) //开辟数据
				}

				if data1 == 1 {
					arr = append(arr, data2)
				}

				if data1 == 0 && data2 == 1 {

					fmt.Println("数组接受完成", arr)

					for i := 0; i < len(arr); i++ {
						out <- arr[i]
					}

					return out
					arr = make([]int, 0, 0) //开辟数据

				}

			}
		}
	}

}
